package com.zk.algorithm;

import java.util.LinkedHashMap;
import java.util.Map;

public class FirstPositionUniqueCharacter {

    /**
     * 字符串: abaccdeff
     *
     * @param str
     * @return
     */
    public char find(String str) {
        if (str == null || str.isEmpty()) {
            return '#';
        }

        // 按照插入顺序
        LinkedHashMap<Character, Integer> countOfCharMap = new LinkedHashMap<>(str.length());

        for (char c: str.toCharArray()) {
            Integer count = countOfCharMap.get(c);
            if (count == null) {
                countOfCharMap.put(c, 1);
            } else {
                countOfCharMap.put(c, count.intValue() + 1);
            }
        }

        // 找出第一个出现次数为 1 的即可
        for (Map.Entry<Character, Integer> entry: countOfCharMap.entrySet()) {
            if (entry.getValue().intValue() == 1) {
                return entry.getKey();
            }
        }

        return '#';
    }

    public static void main(String...args) {
        System.out.println(new FirstPositionUniqueCharacter().find("abaccdeff"));
    }

}
